from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from User import models
class TokenAuth(BaseAuthentication):
    def authenticate(self,request):
        #判断是否在headers携带token
        token = request.META.get("HTTP_TOKEN")
        # token = request._request.GET.get('token')
        TokenObj = models.User_Token.objects.filter(token = token).first()
        if not TokenObj:
            raise exceptions.AuthenticationFailed('用户认证失败')
        #在rest 内部会把他们给request
        return (TokenObj.user,TokenObj)
    def authenticate_header(self,request):
        pass

